package com.xatz.scanner

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import com.xatz.scanner.databinding.ToolbarLayoutBinding

/**
 * author: tdq
 * date: 2021/6/10
 * description: 自定义标题栏
 */
class ToolbarLayout(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val mBinding = ToolbarLayoutBinding.inflate(LayoutInflater.from(context), this)
    var rightText: String? = ""
        set(value) {
            field = value
            mBinding.tvRightMenu.text = value
        }
    var rightTextColor: Int = ContextCompat.getColor(context,R.color.color_387cf9)
        set(value) {
            field = value
            mBinding.tvRightMenu.setTextColor(value)
        }
    var title: String? = ""
        set(value) {
            field = value
            mBinding.tvTitle.text = value
        }
    private var leftText: String? = ""
        set(value) {
            field = value
            mBinding.tvLeftBack.text = value
        }
    private var showRightMenu: Boolean? = true
        set(value) {
            field = value
            mBinding.tvRightMenu.isVisible = value == true
        }
    private var onClick: OnClickListener? = null

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ToolbarLayout)
        leftText = typedArray.getString(R.styleable.ToolbarLayout_leftText)
        rightText = typedArray.getString(R.styleable.ToolbarLayout_rightText)
        val textColor = typedArray.getColor(
            R.styleable.ToolbarLayout_textColor,
            ContextCompat.getColor(context, R.color.color_333)
        )
        title = typedArray.getString(R.styleable.ToolbarLayout_title)
        showRightMenu = typedArray.getBoolean(R.styleable.ToolbarLayout_showRightMenu, true)
        val rightIcon = typedArray.getResourceId(R.styleable.ToolbarLayout_rightIcon, 0)
        val backgroundColor = typedArray.getColor(
            R.styleable.ToolbarLayout_backgroundColor,
            ContextCompat.getColor(context, R.color.white)
        )
        val leftIcon = typedArray.getResourceId(
            R.styleable.ToolbarLayout_leftIcon,
            R.drawable.ic_back
        )
        typedArray.recycle()
        setLeftDrawable(leftIcon)
        setRightDrawable(rightIcon)
        minimumHeight = 44F.px2Dp()
        mBinding.tvTitle.setTextColor(textColor)
        mBinding.tvLeftBack.setTextColor(textColor)
//        mBinding.tvRightMenu.setTextColor(textColor)
        mBinding.tvLeftBack.setOnClickListener {
            if (onClick != null) {
                onClick?.onClick(it)
                return@setOnClickListener
            }
            (getContext() as Activity).finish()
        }

        mBinding.llLayout.setBackgroundColor(backgroundColor)
    }

    fun setLeftDrawable(@DrawableRes icon: Int, isShow: Boolean = true) {
        if (icon != 0) {
            val leftDrawable = AppCompatResources.getDrawable(context, icon)
            leftDrawable?.setBounds(0, 0, leftDrawable.minimumWidth, leftDrawable.minimumHeight)
            mBinding.tvLeftBack.setCompoundDrawables(leftDrawable, null, null, null)
        }
        if (!isShow) {
            mBinding.tvLeftBack.setCompoundDrawables(null, null, null, null)
        }
    }

    private fun setRightDrawable(@DrawableRes icon: Int) {
        if (icon != 0 && showRightMenu == true) {
            val rightDrawable = AppCompatResources.getDrawable(context, icon)
            rightDrawable?.setBounds(0, 0, rightDrawable.minimumWidth, rightDrawable.minimumHeight)
            mBinding.tvRightMenu.setCompoundDrawables(rightDrawable, null, null, null)
        }
    }

    fun setLeftViewOnClick(onClickListener: OnClickListener) {
        this.onClick = onClickListener
    }

    fun setRightViewOnClick(onClickListener: OnClickListener) {
        mBinding.tvRightMenu.setOnClickListener(onClickListener)
    }
}